package com.shenganyuan.smac.task;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.oklib.utils.helper.RxUtil;
import com.oklib.utils.network.http.ViseHttp;
import com.oklib.utils.network.http.callback.ACallback;
import com.oklib.utils.network.http.subscriber.ApiCallbackSubscriber;
import com.oklib.utils.view.SnackbarUtil;
import com.oklib.widget.recyclerview.BaseQuickAdapter;
import com.oklib.widget.recyclerview.BaseViewHolder;
import com.oklib.widget.recyclerview.CoreRecyclerView;
import com.oklib.widget.recyclerview.listener.OnItemClickListener;
import com.shenganyuan.smac.App;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.common.BaseActivity;
import com.shenganyuan.smac.common.beans.ApiTasks;
import com.shenganyuan.smac.common.beans.TaskInfo;
import com.shenganyuan.smac.common.beans.User;
import com.shenganyuan.smac.common.data.Api;
import com.shenganyuan.smac.common.view.RecyclerViewItemDivider;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * 隐患记录
 *
 * @author Damon
 */

public class ActTasks extends BaseActivity {
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.common_toolbar_center_title)
    TextView commonToolbarCenterTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_view)
    CoreRecyclerView recycler;
    private BaseQuickAdapter baseQuickAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.act_tasks;
    }

    @Override
    public void initUI(Bundle savedInstanceState) {
        baseQuickAdapter = new BaseQuickAdapter<TaskInfo, BaseViewHolder>(R.layout.act_task_item) {
            @Override
            protected void convert(BaseViewHolder helper, TaskInfo item) {
                helper.setText(R.id.tv_unit, item.getZydyname());
                helper.setText(R.id.tv_activity, item.getZyhdname());
                helper.setText(R.id.tv_time_start, item.getYxqQ());
                helper.setText(R.id.tv_time_end, item.getYxqZ());
            }
        };
        recycler.init(baseQuickAdapter);
        recycler.getRecyclerView().
                addItemDecoration(new RecyclerViewItemDivider(mContext, LinearLayoutManager.VERTICAL)
                        .setAppearance(20, "#eeeeee"));
        recycler.addOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
        recycler.openRefresh(new CoreRecyclerView.addDataListener() {
            @Override
            public void addData(int page) {
                getServerData();
            }
        });


    }


    @Override
    public void initData() {
        setTitle("我的隐患排查任务");
        getServerData();
    }


    /**
     * 获取隐患信息
     */
    private void getServerData() {
        User user = App.getCurrentUser();
        ViseHttp.RETROFIT().create(Api.class).getTasks(user.getId(), App.getAccessToken())
                .compose(RxUtil.rxSchedulerHelper()).subscribe(new ApiCallbackSubscriber<>(new ACallback<ApiTasks>() {
            @Override
            public void onSuccess(ApiTasks data) {
                try {
                    if ("true".equalsIgnoreCase(data.getResult())) {
                        recycler.getAdapter().setNewData(data.getRwfp());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                SnackbarUtil.showShort(getWindow().getDecorView(), "获取隐患排查任务信息失败");
            }
        }));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
